<?php
defined('_JEXEC') or die('Restricted access');
jimport( 'joomla.html.html.tabs' );
JHtml::_('jquery.framework');
JHtml::_('behavior.keepalive');
JHTML::_('behavior.tooltip'); 
JHtml::_('behavior.framework');

?>
<link rel="stylesheet" type="text/css" href="<?php echo JURI::root();?>js/multiselect/jquery-ui.css" />
<link type="text/css" href="<?php echo JURI::root();?>/css/buttons.css" rel="stylesheet" />
<!-- Date picker -->
<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" />
<link href="<?php echo JURI::root();?>administrator/components/com_wrd/assets/css/jquery-ui-1.9.2.custom.css" rel="stylesheet">
<script src="<?php echo JURI::root();?>administrator/components/com_wrd/assets/js/jquery-ui-1.9.2.custom.js"></script>
<!--End -->
<script type="text/javascript">
    jQuery(document).ready(function(){
		jQuery( "#date_receipt" ).datepicker(
			{  
				maxDate: 0,
				showOn: "button",
				buttonImage: "<?php echo JURI::root();?>administrator/components/com_wrd/assets/images/calendar.png",
				buttonImageOnly: true,
				changeMonth: true,
				changeYear: true,
				minDate: new Date(2000, 1 - 1, 1),
				dateFormat: 'yy-mm-dd'
			}
		);

		
		<?php if($this->data->id != 0): ?>
		$("#outlet option").not(":selected").attr("disabled", "disabled");
		$("#hour option").not(":selected").attr("disabled", "disabled");
		$("#minute option").not(":selected").attr("disabled", "disabled");

		jQuery( "#expire" ).datepicker({  
			
			showOn: "button",
			buttonImage: "<?php echo JURI::root();?>administrator/components/com_wrd/assets/images/calendar.png",
			buttonImageOnly: true,
			changeMonth: true,
			changeYear: true,
			minDate: new Date(2000, 1 - 1, 1),
			dateFormat: 'yy-mm-dd'
		});

		<?php endif; ?>
		
	});
	
	function checkTransaction(t){
		jQuery("document").ready(function(){
				var transaction_id = t;
				if(t=='') {jQuery("#warning").css('color','red').html("Warning: Please enter transaction");}
				jQuery.ajax({
					url: "index.php?option=com_wrd&task=checkTransaction",
					type: "POST",
					data: 'transaction_id='+transaction_id,
					success: function (res) {
						if(res>0){jQuery("#warning").css('color','red').html("Warning: Transaction exist");}
						else if((t!='')&&(res==0)){
							jQuery("#warning").css('color','#35c00f').html("OK.Transaction maybe used");
							
						}
						jQuery("#ctransaction").val(res);
					}		
				});
		});
	}
	
	jQuery(function(){
        jQuery("#netsale").click(function(){
            calculateNetSale();
        });
        jQuery("#total").click(function(){
            calculateTotal();
        });
        
        /*** Net sale ***/
        bindEventChangeNetsale("food");
        bindEventChangeNetsale("beverage");
        bindEventChangeNetsale("liquor");
        bindEventChangeNetsale("other");
        
        /*** Total ***/
        bindEventChangeTotal("netsale");
        bindEventChangeTotal("sc");
        bindEventChangeTotal("vat");
    });
    
    function bindEventChangeNetsale(objName){
        jQuery("#"+objName).change(function(){
            calculateNetSale();
        });
    }
    
    function bindEventChangeTotal(objName){
        jQuery("#"+objName).change(function(){
            calculateTotal();
        });
    }
    
    function calculateTotal(){
        var total = parseFloat(jQuery("#netsale").val() != '' ? jQuery("#netsale").val() : 0) + parseFloat(jQuery("#sc").val() != '' ? jQuery("#sc").val() : 0);
        total = total + parseFloat(jQuery("#vat").val() != '' ? jQuery("#vat").val() : 0);
        jQuery("#total").val(total);
    }
    
    function calculateNetSale(){
        var netsale = parseFloat(jQuery("#food").val() != '' ? jQuery("#food").val() : 0) + parseFloat(jQuery("#beverage").val() != '' ? jQuery("#beverage").val() : 0);
        netsale = netsale + parseFloat(jQuery("#liquor").val() != '' ? jQuery("#liquor").val() : 0) + parseFloat(jQuery("#other").val() != '' ? jQuery("#other").val() : 0);
        jQuery("#netsale").val(netsale);
    }
    
	//<![CDATA[
	Joomla.submitbutton = function(pressbutton){
		if (pressbutton == 'cancel') {
			submitform( pressbutton );
			return;
		}
		if (pressbutton == 'save') {
			if (document.adminForm.transaction_id.value == "") {
                alert( 'Please fill transaction id!' );
                document.adminForm.transaction_id.focus();
                return false;
            }
			if(jQuery("#ctransaction").val()>0){
				jQuery("#warning").html("Warning: Please choose a different transaction");
				return false;
			}
            if (document.adminForm.outlet.value == "") {
                alert( 'Please select outlet!' );
                document.adminForm.outlet.focus();
                return false;
            }
            
            submitform(pressbutton );
		}
	}
	
	  function isNumberKey(evt,v)
	  {
		 if(evt.which == 8) return true;
		 if(v.length > 15) {
			alert("You should not type number too big :)");
			return false;
		 }
		 var charCode = (evt.which) ? evt.which : event.keyCode
		 if (charCode > 31 && (charCode < 48 || charCode > 57))
			return false;
		 return true;
	  }
	  
</script>

<?php 
	if($this->data->id != 0)
		$style = 'readonly="readonly"';
?>
<form action="<?php echo JRoute::_("index.php?option=com_wrd&controller=atransaction")?>" method="post" name="adminForm"  id="adminForm" >
		<?php 
   			echo JHtml::_('bootstrap.startTabSet', 'atransaction', array('active' => 'detail')); 
   			echo JHtml::_('bootstrap.addTab', 'atransaction', 'detail', 'Details');
   		?>
	    
	    	
			<table class="table table-striped adminlist">
				<tr>
					<td class="key">
						<label for="title"><?php echo JText::_('Vip number')?>:</label>
					</td>
					<td colspan="5">
						<input type="text" title="type" readonly="readonly" maxlength="50" size="50" value="<?php if(empty($this->vip_num)){ $this->vip_num = '' ; } echo empty($this->data->vip_number)? $this->vip_num : $this->data->vip_number ; ?>" style="background: #f4f4f4; width: 163px;" id="vip_number" name="vip_number" class="vip_number"/>  
					</td>
				</tr>
				<tr>
					<td class="key">
						<label for="title"><?php echo JText::_('Transaction id')?>:</label>
					</td>
					<td colspan="5">
						<input <?php echo $style; ?> type="text" title="Type is number" onkeypress="return isNumberKey(event,this.value)" onkeyup="return checkTransaction(this.value)"  maxlength="50" style="width: 163px;"  size="50" value="<?php echo $this->data->transaction_id; ?>" id="transaction_id" name="transaction_id" class="transaction_id"/>  
						<div style="font-size: 13px;float: right;font-weight: bold;height: 20px;padding-top: 9px;width: 332px;" id="warning"></div>
					</td>
				</tr>
				<tr>
					<td class="key">
						<label for="title"><?php echo JText::_('Date receipt')?>:</label>
					</td>
					<td>
						<?php 
						
						 
						if($this->data->date_receipt != '0000-00-00 00:00:00' || $this->data->date_receipt != '')
							$date_receipt =  date('Y-m-d',strtotime($this->data->date_receipt)); 
						else
							echo '';
						
						?>
						<?php if($this->data->id == 0): ?>
							<input <?php echo $style; ?> type="text" id="date_receipt" name="date_receipt" size="30" value="<?php echo $date_receipt;?> "/>
						<?php else: ?>
							
							<input <?php echo $style; ?> type="text" name="date_receipt" size="30" value="<?php echo $date_receipt;?> "/>
						
						<?php endif; ?>
					</td>
					
					
					<td width="5%">
						<?php echo JText::_('Hour: ')?>
					</td>
					<td width="18%">
						<?php echo JHTML::_('wrd.listHours','hour',date('H',strtotime($this->data->date_receipt)),'','hour',0); ?>
					</td>
					<td width="6%">
						<?php echo JText::_('Minute: ')?>
					</td>
					<td width="10%">
						<?php echo JHTML::_('wrd.listMinute','minute',date('i',strtotime($this->data->date_receipt)),'','minute',0); ?>
					</td>
				</tr>
				<?php if($this->data->id != 0): ?>
				<tr>
					<td class="key">
						<label for="title"><?php echo JText::_('Date Expired')?>:</label>
					</td>
					<td>
						<input type="text" id="expire" name="expire" size="30" 
						value="<?php echo $this->point_detail->expire; ?>"/>
					</td>
				</tr>
				<?php endif; ?>
				<tr>
					<td class="key">
						<label><?php echo JText::_('Outlet')?>:</label>
					</td>
					<td colspan="5">
						<?php echo $this->lists['outlets'] ; ?>
					</td>
				</tr>
            </table>
            <div class="ui-accordion ui-widget ui-helper-reset ui-accordion-icons" style="margin-top: 15px;" id="update_transaction_panel">
                <div>
                    <h3 class="ui-accordion-header ui-helper-reset ui-state-default ui-state-active ui-corner-top"><span class="ui-icon ui-icon-triangle-1-s"></span><a tabindex="-1" href="#">More</a></h3>
                    <div class="ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom ui-accordion-content-active" id="update_transaction_panel_content">
                        <table cellpadding="0" cellspacing="0" width="100%">
                            <tr>
                                <td class="key">
                                    <label for="title"><?php echo JText::_('Pax')?>:</label>
                                </td>
                                <td colspan="2">
                                    <input <?php echo $style; ?> onkeypress="return isNumberKey(event,this.value)" type="text" title="Type is number" maxlength="50" size="50" value="<?php echo $this->data->pax; ?>" id="pax" name="pax" class="pax"/>  
                                </td>
                            </tr>
                            <tr>
                                <td class="key">
                                    <label for="title"><?php echo JText::_('Food')?>:</label>
                                </td>
                                <td colspan="2">
                                    <input <?php echo $style; ?> onkeypress="return isNumberKey(event,this.value)"  type="text" title="Type is number" maxlength="50" size="50" value="<?php echo $this->data->food; ?>" id="food" name="food" class="food"/>  
                                </td>
                            </tr>
                            <tr>
                                <td class="key">
                                    <label for="title"><?php echo JText::_('Beverage')?>:</label>
                                </td>
                                <td colspan="2">
                                    <input <?php echo $style; ?> type="text" onkeypress="return isNumberKey(event,this.value)" title="Type is number" maxlength="50" size="50" value="<?php echo $this->data->beverage; ?>" id="beverage" name="beverage" class="beverage"/>  
                                </td>
                            </tr>
                            <tr>
                                <td class="key">
                                    <label for="title"><?php echo JText::_('Liquor')?>:</label>
                                </td>
                                <td colspan="2">
                                    <input <?php echo $style; ?> type="text" onkeypress="return isNumberKey(event,this.value)" title="Type is number" maxlength="50" size="50" value="<?php echo $this->data->liquor; ?>" id="liquor" name="liquor" class="liquor"/>  
                                </td>
                            </tr>
                            <tr>
                                <td class="key">
                                    <label for="title"><?php echo JText::_('Other')?>:</label>
                                </td>
                                <td colspan="2">
                                    <input <?php echo $style; ?> type="text" onkeypress="return isNumberKey(event,this.value)" title="Type is number" maxlength="50" size="50" value="<?php echo $this->data->other; ?>" id="other" name="other" class="other"/> 
                                </td>
                            </tr>
                            <tr>
                                <td class="key">
                                    <label for="title"><?php echo JText::_('Discount')?>:</label>
                                </td>
                                <td colspan="2">
                                    <input <?php echo $style; ?> type="text" onkeypress="return isNumberKey(event,this.value)" title="Type is number" maxlength="50" size="50" value="<?php echo $this->data->discount; ?>" id="discount" name="discount" class="discount"/>  
                                </td>
                            </tr>
                            <tr>
                                <td class="key">
                                    <label for="title"><?php echo JText::_('Netsale')?>:</label>
                                </td>
                                <td colspan="2">
                                    <input <?php echo $style; ?> type="text" title="type" readonly="readonly" style="background: #f4f4f4; width: 150px;" maxlength="50" size="50" value="<?php echo $this->data->netsale; ?>" id="netsale" name="netsale" class="netsale"/> 
                                </td>
                            </tr>
                            <tr>
                                <td class="key">
                                    <label for="title"><?php echo JText::_('SC')?>:</label>
                                </td>
                                <td colspan="2">
                                    <input <?php echo $style; ?> type="text" onkeypress="return isNumberKey(event,this.value)" title="Type is number" maxlength="50" size="50" value="<?php echo $this->data->sc; ?>" id="sc" name="sc" class="sc"/>  
                                </td>
                            </tr>
                            <tr>
                                <td class="key">
                                    <label for="title"><?php echo JText::_('VAT')?>:</label>
                                </td>
                                <td colspan="2">
                                    <input <?php echo $style; ?> type="text" onkeypress="return isNumberKey(event,this.value)" title="Type is number" maxlength="50" size="50" value="<?php echo $this->data->vat; ?>" id="vat" name="vat" class="vat"/>  
                                </td>
                            </tr>
                            <tr>
                                <td class="key">
                                    <label for="title"><?php echo JText::_('Total')?>:</label>
                                </td>
                                <td colspan="2">
                                    <input <?php echo $style; ?> type="text" title="type" readonly="readonly" style="background: #f4f4f4; width: 150px;" maxlength="50" size="50" value="<?php echo $this->data->total; ?>" id="total" name="total" class="total"/>
                                </td>
                            </tr>
                            <tr>
                                <td class="key">&nbsp;</td>
                                <td colspan="2"><br />
                                    <a style="cursor: pointer;" onclick="javascript:Joomla.submitbutton('save')" id="update_transaction" class="ui-state-default ui-corner-all special-link"><span class="ui-icon ui-icon-newwin"></span>Save</a>
                                </td>
                            </tr>
                        </table>        
                    </div>
                </div>
            </div>
	<?php 
		echo JHtml::_('bootstrap.endTab');
		if(!empty($this->user_info)):
		echo JHtml::_('bootstrap.addTab', 'atransaction', 'infotab', 'User Infomation');
	?>
	<div id="stats" style="float:right;width:40%">
		
				<table>
					<tr>
						<td><?php echo JText::_('Member ID') ;?></td>
						<td>:</td>
						<td><strong>
							<?php
								if(!empty($this->user_info->member_id)){
									echo $this->user_info->member_id;
								}
							?> 
							</strong>
						</td>
					</tr><tr>	
						<td><?php echo JText::_('Name') ;?></td>
						<td>:</td>
						<td>
							<?php
								if(!empty($this->user_info->f_name) || !empty($this->user_info->l_name)){
									$guest_name = $this->user_info->f_name." ".$this->user_info->l_name ;
								}else{
									$guest_name = $this->data->guest_name ;
								}
								echo $this->user_info->prefix.' '.$guest_name ; 
							?> 
						</td>
					</tr>
					<tr>
						<td><?php echo JText::_('Date of Birth') ;?></td>
						<td>:</td>
						<td>
							<?php
								echo $this->user_info->dob ; 
							?> 
						</td>
					</tr>
					<?php if(!empty($this->job)): ?>
					<tr>
						<td><?php echo JText::_('Job Title') ;?></td>
						<td>:</td>
						<td>
							<?php
								echo empty($this->job) ? '' : $this->job ; 
							?> 
						</td>
					</tr>
					<?php endif; ?>
					<tr>
						<td><?php echo JText::_('Member Joining Date') ;?></td>
						<td>:</td>
						<td>
							<?php
								echo $this->user_info->created_date ; 
							?> 
						</td>
					</tr>
					<?php if(!empty($this->outlet->name)): ?>
					<tr>
						<td><?php echo JText::_('Outlet') ;?></td>
						<td>:</td>
						<td>
							<?php
								echo empty($this->outlet->name) ? '' : $this->outlet->name ; 
							?> 
						</td>
					</tr>
					<?php endif;?>
					<tr>
						<td><?php echo JText::_('Current points balance') ;?></td>
						<td>:</td>
						<td>
							<?php
								echo number_format($this->infotest[0]->point, 0,',',',');
							?> 
						</td>
					</tr>
                    <tr>
						<td><?php echo JText::_('Total Spending') ;?></td>
						<td>:</td>
						<td>
							<?php
								echo number_format($this->user_info->total_spend, 0,',',',');
							?> 
						</td>
					</tr>
				</table>
			<?php 
			echo JHtml::_('bootstrap.endTab');
		?>
	</div>
	<?php endif ;?>
	<input type="hidden" id="ctransaction" value="" />
	<input type="hidden" name="vip_num" value="<?php if(empty($this->vip_num)){ $this->vip_num = '' ; } echo empty($this->data->vip_number)? $this->vip_num : $this->data->vip_number ; ?>" />
	<input type="hidden" name="created_id" value="<?php echo $this->user->id;?>" />
	<input type="hidden" name="forward" value="<?php echo $this->forward ; ?>" />
	<input type="hidden" name="profile_id" value="<?php echo $this->user_info->id; ?>" />
	<input type="hidden" name="controller" value="atransaction" />
	<input type="hidden" name="option" value="com_wrd" />
	<input type="hidden" name="id" value="<?php echo (int)$this->data->id; ?>" />
	<input type="hidden" name="point_id" value="<?php echo (int)$this->point_detail->id; ?>" />
	<input type="hidden" name="task" value="" />
	<?php echo JHtml::_('bootstrap.endTabSet'); echo JHTML::_( 'form.token' ); ?>
</form>